/*
 * @Date: 2025-09-09 11:23:16
 * @Auth: 873768511@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-09-30 18:33:12
 * @FilePath: \corpus\src\pages\resourceManagement\list\component\CorpusDawer\index.tsx
 */
import {
  CheckCircleFilled,
  ClockCircleFilled,
  CloseCircleFilled,
  MinusCircleFilled,
} from '@ant-design/icons';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { Drawer, message, Space } from 'antd';
import hooks from '../../hooks';
import useProTableConfig from '../../hooks/useProTableConfig';
import styles from './index.less';

export default () => {
  const {
    corpusDrawerDetail,
    corpusDrawerDetailToggle,
    getRetryCorpusFileTask,
    getCorpusFileTaskList,
  } = hooks.useContainer();
  const { setJsonViewVisible, setViewVisible, actionRef, setUrl, setType } = useModel(
    'useTree',
    (model) => {
      return {
        setJsonViewVisible: model.setJsonViewVisible,
        setViewVisible: model.setViewVisible,
        setUrl: model.setUrl,
        setType: model.setType,
        actionRef: model.actionRef,
      };
    },
  );
  const proTableConfig = useProTableConfig({
    optionRender(config, props, dom) {
      return [...dom.reverse()];
    },
  });
  const columns: ProColumns<any>[] = [
    {
      title: '序号',
      width: 60,
      render: (_, __, index) => index + 1,
    },

    {
      title: '入库环境',
      width: 126,
      dataIndex: 'type',
      render: (_, record) => {
        return (
          <>
            {record.type === 'PARSE' ? '文档解析' : record.type === 'SPLIT' ? '拆分文本' : '问答对'}
          </>
        );
      },
    },
    {
      title: '描述',
      dataIndex: 'status',
      ellipsis: false,
      width: 160,
      order: 1,
      valueType: 'select',

      renderText: (_, record) => {
        return (
          <>
            <span key={1}>
              {record?.status === 3 ? (
                <ClockCircleFilled style={{ color: 'green' }} />
              ) : record?.status === 'PARSING_SUCCESS' ? (
                <CheckCircleFilled style={{ color: 'orange' }} />
              ) : record?.status === 'WAIT_PARSING' ? (
                <MinusCircleFilled style={{ color: '#1890ff' }} />
              ) : record?.status === 'PARSING_FAIL' ? (
                <CloseCircleFilled style={{ color: 'red' }} />
              ) : (
                <CloseCircleFilled style={{ color: 'yellow' }} />
              )}
              <span style={{ marginLeft: '5.0px' }}>
                {record.status === 'PARSING_SUCCESS'
                  ? '生成成功'
                  : record.status === 'WAIT_PARSING'
                  ? '等待生成'
                  : record.status === 'PARSING_FAIL'
                  ? '生成失败'
                  : record.status === 'WAIT_PARSING'
                  ? '待生成'
                  : '不解析'}
              </span>
            </span>
          </>
        );
      },
    },
    {
      title: '创建时间',
      width: 120,
      dataIndex: 'createTime',
    },
    {
      title: '完成时间',
      width: 120,
      dataIndex: 'completionTime',
    },

    {
      title: '操作',
      width: 126,
      dataIndex: 'operate',
      hideInSearch: true,
      renderText: (text, record) => {
        return (
          <Space size="small" className={styles.spaceWrap}>
            {record.status !== 'PARSING_FAIL' && (
              <a
                onClick={() => {
                  setUrl(record?.fileId);
                  setType(record?.suffix === 'doc' ? 'docx' : record?.suffix);
                  if (record?.suffix?.indexOf('json') > -1 || record?.suffix === 'txt') {
                    setJsonViewVisible(true);
                  } else if (
                    record?.suffix === 'docx' ||
                    record?.suffix === 'pdf' ||
                    record?.suffix === 'doc'
                  ) {
                    setViewVisible(true);
                  } else {
                    message.warning('暂不支持预览');
                  }
                }}
              >
                预览
              </a>
            )}
            {record.status !== 'PARSING_FAIL' && (
              <a
                onClick={() => {
                  window.open(
                    `${window.location.origin}/corpus/common/downloadFile?fileId=${record?.fileId}`,
                  );
                }}
              >
                下载
              </a>
            )}
            {record.status === 'PARSING_FAIL' && (
              <a
                onClick={() => {
                  getRetryCorpusFileTask.runAsync(record?.id);
                }}
              >
                重试
              </a>
            )}
          </Space>
        );
      },
    },
  ];

  return (
    <div className={styles.detailContainer}>
      <Drawer
        title="语料库详情"
        destroyOnHidden={true}
        width={900}
        open={corpusDrawerDetail}
        onClose={corpusDrawerDetailToggle}
        // footer={
        //   <Space>
        //     <Button
        //       onClick={() => {
        //         drawerDetailToggle();
        //       }}
        //     >
        //       取消
        //     </Button>
        //   </Space>
        // }
      >
        {/* <TableData data={getCorpusFileTaskList.data} columns={columns} /> */}
        <ProTable
          {...proTableConfig}
          actionRef={actionRef}
          columns={columns}
          request={getCorpusFileTaskList.runAsync}
          size="middle"
          rowKey="id"
          ghost={true}
          search={false}
        />
      </Drawer>
    </div>
  );
};
